(function($, doc) {
	$.init({});		
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    
    window.onload = function(){ 
    	refresh_data();

		$.plusReady(function() {
			//关闭等待框
			plus.nativeUI.closeWaiting();
			//显示当前页面
			mui.currentWebview.show();		
			
			mui("#main-area").on('tap',' .mui-table-view-cell a',function(){
				var bank_id=this.getAttribute('data-id');
				//打开银行卡页
				mui.openWindow({
					id: 'addcard.html',
					url: 'addcard.html',
					extras: {
						bank_id: bank_id//自定义扩展参数，可以用来处理页面间传值
					},
					show: {
						autoShow: false
					},
					waiting: {
						autoShow: true,
						title: '正在加载...'
					}
				});
			})
			
			
			doc.getElementById('add_bank').addEventListener('tap', function() {
				//打开银行卡页
				mui.openWindow({
					id: 'addcard.html',
					url: 'addcard.html',
					extras: {
						bank_id: 0//自定义扩展参数，可以用来处理页面间传值
					},
					show: {
						autoShow: false
					},
					waiting: {
						autoShow: true,
						title: '正在加载...'
					}
				});
			})

			
			//删除
			mui("#main-area").on('tap',' .card_num button',function(){
				var bank_id=this.getAttribute('data-id');
				var btnArray = ['确定', '取消'];
				mui.confirm('您确定删除该银行卡？', '', btnArray, function(e) {
					if (e.index == 0) {
						mui.ajax({
							url: appurl + "Bank/del_bank/",
							data: {'user_id':uid,'bank_id':bank_id},
							async: true,
							dataType: 'json',
							crossDomain: true, //强制使用5+跨域
							type: 'post',
							timeout: 10000,
							success: function(res) {
								console.log(JSON.stringify(res));
								// 请求成功
								if(res.status<1){
									plus.nativeUI.toast(res.msg);
								}
							    else{
							    	plus.nativeUI.toast(res.msg);
							    	mui.fire(plus.webview.getWebviewById('payment_method.html'), 'refresh_data');
							    	mui.fire(plus.webview.getWebviewById('transaction.html'), 'refresh_bank');
							    	refresh_data();
							    }
							},
							error: function(xhr, type, errorThrown) {
								//console.log(JSON.stringify(xhr));
								//console.log(JSON.stringify(type));
								//console.log(JSON.stringify(errorThrown));
								mui.toast('提交失败！');
							}
						});
					} else {
						//
					}
				})
				
			})
			
			
			window.addEventListener('refresh_data',function(event){
			    refresh_data();
			});
			
		});
		
	}
	
	
	function  refresh_data(){
		mui.ajax({
		    url: appurl + "Bank/get_bank/",
			data: {
				'user_id': uid,
				'etype': 1
			},
			async: true,
			dataType: 'json',
			crossDomain: true, //强制使用5+跨域
			type: 'get',
			timeout: 10000,
			success: function(res) {
				console.log(JSON.stringify(res));
				// 请求成功
				doc.querySelector("#main-area").innerHTML = template('main-template', {
					'data': res.data
				});
		
			},
			error: function(xhr, type, errorThrown) {
				//console.log(JSON.stringify(xhr));
				//console.log(JSON.stringify(type));
				//console.log(JSON.stringify(errorThrown));
				mui.toast('提交失败！');
			}
		});
	}
		

}(mui, document));